package com.zyiot.lbs;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.zyiot.Dao.MessageDAO;
import com.zyiot.adapter.MessagesAdapter;
import com.zyiot.utils.CommonUtil;
import com.zyiot.utils.FileHelper;

public class TabMessagesActivity extends Activity {
	private Context context;
	MessageDAO messagedao;
	SimpleAdapter newslistAdapter = null;
	MessagesAdapter messageAdapter = null;
	List<HashMap<String, Object>> msg_datas;
	private LinearLayout ll_cb_buttons;
	private CheckBox msg_cb_select;

	private int checkNum; // 记录选中的条目数量
	CheckBox msg_cb_selectall;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.layout_messages);
		messagedao = new MessageDAO(this);
		int count = (int) messagedao.getCount();
		msg_datas = messagedao.getScrollDataNewsMap(0, count);
		ListView newslist = (ListView) findViewById(R.id.list_news);

		ll_cb_buttons = (LinearLayout) findViewById(R.id.ll_cb_buttons);
		// msg_cb_select = (CheckBox) findViewById(R.id.msg_cb_select);
		// msg_cb_select.setVisibility(View.GONE);

		messageAdapter = new MessagesAdapter(context, msg_datas);
		newslist.setAdapter(messageAdapter);

		newslist.setOnItemClickListener(tvlist_msg_item_listener);
		Button btn_top_msg_delete = (Button) findViewById(R.id.top_msg_delete);
		btn_top_msg_delete.setOnClickListener(btn_top_delete_msg_listener);

		// 隐藏按钮
		Button msg_cancel = (Button) findViewById(R.id.msg_cancel);
		msg_cancel.setOnClickListener(btn_msg_cancel_listener);

		Button msg_delete = (Button) findViewById(R.id.msg_delete);
		msg_delete.setOnClickListener(btn_msg_delete_listener);

		msg_cb_selectall = (CheckBox) findViewById(R.id.msg_cb_selectall);
		msg_cb_selectall.setOnCheckedChangeListener(cb_dmsg_cb_delete_listener);

	}

	private OnClickListener btn_top_delete_msg_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (msg_datas.size() != 0) {
				new AlertDialog.Builder(context).setMessage(R.string.delete_msg_info)
						.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int whichButton) {
								deleteall();
							}
						}).setNegativeButton(R.string.button_cancel, null).show();

				// ll_cb_buttons.setVisibility(View.VISIBLE);
			} else {
				Toast.makeText(context, R.string.msg_empty_info, Toast.LENGTH_SHORT).show();
			}
		}

	};
	private OnClickListener btn_msg_cancel_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			ll_cb_buttons.setVisibility(View.GONE);
			deselectall();
			msg_cb_selectall.setChecked(false);
		}
	};
	private OnClickListener btn_msg_delete_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			deleteall();
		}
	};
	private OnCheckedChangeListener cb_dmsg_cb_delete_listener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if (isChecked) {
				selectall();
			} else {
				deselectall();
			}
		}

	};
	private OnItemClickListener tvlist_msg_item_listener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			String URL = (String) msg_datas.get(position).get("msg_url");
			if (URL != null) {
				int version = Integer.valueOf(VERSION.SDK_INT);
				Intent intent = new Intent();
				intent.setClass(context, Webview.class);
				intent.putExtra("url", URL);
				startActivity(intent);
				if (version >= 5) {
					overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
				}
			}
		}

	};

	private void deleteall() {
		Iterator<HashMap<String, Object>> iterator = msg_datas.iterator();
		Vector<Integer> ids = new Vector<Integer>();
		while (iterator.hasNext()) {
			HashMap<String, Object> temp = iterator.next();
			// if (temp.get("msg_flag").equals("true")) {
			ids.add((Integer) temp.get("msg_id"));
			iterator.remove();
			// }
		}
		Integer msg_ids[] = new Integer[ids.size()];
		for (int i = 0; i < ids.size(); i++) {
			msg_ids[i] = ids.elementAt(i);
		}
		// 删除本地网页文件
		String strRuchTextFile = CommonUtil.getRootFilePath() + "/Android/data/" + getPackageName() + "/";
		FileHelper.deleteDirectory(strRuchTextFile);
		// 删除数据库
		messagedao.detele(msg_ids);
		checkNum = 0;
		// 通知列表数据修改
		dataChanged();

	}

	// 刷新listview和TextView的显示
	private void dataChanged() {
		// 通知listView刷新
		messageAdapter.notifyDataSetChanged();

	}

	private void deselectall() {
		// 遍历list的长度，将已选的按钮设为未选
		for (int i = 0; i < msg_datas.size(); i++) {
			if (msg_datas.get(i).get("msg_flag").equals("true")) {
				msg_datas.get(i).put("msg_flag", "false");
				checkNum--;// 数量减1
			}
		}
		// 刷新listview和TextView的显示
		dataChanged();
	}

	private void selectall() {
		// 遍历list的长度，将MyAdapter中的map值全部设为true
		for (int i = 0; i < msg_datas.size(); i++) {
			msg_datas.get(i).put("msg_flag", "true");
		}
		// 数量设为list的长度
		checkNum = msg_datas.size();
		// 刷新listview和TextView的显示
		dataChanged();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
